﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Jinjia.Approve.Entity.Entity.JJFlow;
using Jinjia.Approve.IRepository.JJFlow;
using Jinjia.Core.UnitOfWork.UnitOfWorkComponent;
using SqlSugar;

namespace Jinjia.Approve.Repository.JJFlow
{
    public class FlowNodeEmpRepository : IFlowNodeEmpRepository
    {
        private readonly ISqlSugarClient _db;

        public FlowNodeEmpRepository(IUnitOfWork unitOfWork)
        {
            _db = unitOfWork.GetDbClient();
        }

        public async Task<List<jj_flow_node_emp>> GetByNodeId(int nodeId, int templateId)
        {
            return await _db.Queryable<jj_flow_node_emp>().Where(x => x.node_id == nodeId).Where(x => x.template_id == templateId).ToListAsync();
        }

        public async Task PhysicalDelete(int id)
        {
            await _db.Deleteable<jj_flow_node_emp>()
                .Where(x => x.id == id)
                .ExecuteCommandAsync();
        }

        public async Task PhysicalDelete(List<int> ids)
        {
            await _db.Deleteable<jj_flow_node_emp>()
                .Where(x => ids.Contains(x.id))
                .ExecuteCommandAsync();
        }

        public async Task<int> Save(jj_flow_node_emp data)
        {
            if (data.id <= 0) //新增
            {
                var id = await _db.Insertable(data).ExecuteReturnIdentityAsync();
                return id;
            }
            else
            {
                await _db.Updateable(data)
                    .Where(x => x.id == data.id)
                    .ExecuteCommandAsync();
                return data.id;
            }
        }

        public async Task Save(List<jj_flow_node_emp> data)
        {
            var insertData = data.Where(x => x.id <= 0).ToList();

            if (insertData.Any())
            {
                await _db.Insertable(insertData).ExecuteCommandAsync();
            }

            var updateData = data.Where(x => x.id > 0).ToList();

            if (updateData.Any())
            {
                await _db.Updateable(insertData).ExecuteCommandAsync();
            }
        }

        public async Task<List<int>> GetUserIdByNodeId(int template_id, int node_id)
        {
            return await _db.Queryable<jj_flow_node_emp>()
                .Where(x => x.template_id == template_id && x.node_id == node_id)
                .Select(p => p.user_id)
                .ToListAsync();
        }

        public async Task<List<string>> GetRoleCodeByNodeId(int template_id, int node_id)
        {
            return await _db.Queryable<jj_flow_node_emp>()
                .Where(x => x.template_id == template_id && x.node_id == node_id)
                .Select(p => p.role_code)
                .ToListAsync();
        }

        public async Task<bool> ChangeApprover(int handoverUserId, int userId)
        {
            return await _db.Updateable<jj_flow_node_emp>()
                .SetColumns(m => m.user_id == handoverUserId)
                .Where(m => m.user_id == userId)
                .ExecuteCommandHasChangeAsync();
        }
    }
}